<?php
require_once 'BaseController.php';

/**
 * 【PC版】物件詳細 コントローラ
 */
class Renew_DetailController extends Renew_BaseController
{
    /**
     * メインアクション
     */
    public function indexAction()
    {
        $params = $this->getParameters();
        $typ = $params['Type'];
        $code = $params['Code'];

        $this->makeTemplateParams($code);

        if (empty($this->_data['buken']['T03_HEYA_CD']))
        {
            error_log('no bukken');
            // $this->my_redirect('sitemap/404error.html');
            /*
             * $this->getResponse()->setHttpResponseCode(404); $this->_404_header = true;
             * header("HTTP/1.1 404 Not Found"); Apm_Util::outputXmlHttpHeader404();
             * Apm_Util::outputCMSData404($html,$this->_property);
             */
            return;
        }

        $this->displayBody2($typ);
    }

    private function makeTemplateParams($code)
    {
        $model = new Rstdtl_Model_Bukendtl();
        $model_campaign = new Renew_Model_BukkenCampaign();
        $model_shop = new Renew_Model_BukkenTenpo();

        $config = array();
        $params = array();

        $params['ID'] = $code;
        $params['buken_static'] = '1';
        $params['HeyaCd'] = $code;

        $data = $model->getBukenDtlListData($params);

        $model->convertData($config, $data, $params);

        /*
         * if(empty($data[0]['T03_HEYA_CD'])) { error_log('no bukken');
         * $this->getResponse()->setHttpResponseCode(404); $this->_404_header = true;
         * header("HTTP/1.1 404 Not Found"); Apm_Util::outputXmlHttpHeader404();
         * Apm_Util::outputCMSData404($html,$this->_property); return; }
         */

        $index = 1;
        foreach ($data[0]['BUKEN_IMAGE'] as $k => $v)
        {
            foreach ($v as $k1 => $v1)
            {
                $data[0]['BUKEN_IMAGE'][$k][$k1]['MODAL_INDEX'] = sprintf("%02d", $index);
                $index++;
            }
        }
        // $this->test($data[0]['BUKEN_IMAGE']);
        // $tenpo1 = $model_shop->getTenpoList($data[0]['T03_DAIHYO_HEYA_CD']);
        $tenpo1 = $model_shop->getTenpoList($code);

        // 代表物件でない場合は0件
        if (count($tenpo1) == 0)
        {
            $tenpo1 = $model_shop->getTenpoListHeya($code);
        }
        $data[0]['tenpo1'] = $tenpo1;

        $heya_campaigns = $model_campaign->getHeyaCampaigns($code);
        $norm_campaigns = $model_campaign->getNormalCampaigns();

        $data[0]['campaigns'] = array_merge($heya_campaigns, $norm_campaigns);

        // CALLNOTE
        $lineUrl = '';
        $lineId = '';

        foreach ($data[0]['tenpo1'] as $i => $tenpo)
        {
            $tempData = Apm_ShopUtil::getCallingData($tenpo, Apm_Config_Shop::FREE_CONNECTION_NO_1);
            $data[0]['tenpo1'][$i]['IS_FREE_CALL'] = $tempData['IS_FREE_CALL'];
            if ($tempData['IS_FREE_CALL'])
            {
                $data[0]['tenpo1'][$i]['FREE_CALLING'] = $tempData['TEL_01'];
                $data[0]['tenpo1'][$i]['CONVERT_CONNECTION_NO'] = Apm_Bukkenitems::GetBukkenWebCallingNo($tenpo, Apm_Config_Rstdtl::CAMPAIGN_PC);
            }

            // ショップID
            if ($tenpo['LINE_URL'] != '' && $lineUrl == '')
            {
                $lineUrl = $tenpo['LINE_URL'];
                $lineId = $tenpo['M05_TENPO_LINE_ID'];
            }
            // ショップID
        }

        // PV増分処理用(代表物件の場合も考慮し、取り扱い店舗のみカウント)
        $model->S_RegPageViewLog($data[0]["M05_TENPO_CD"], 0, 0, $data[0]['T03_ORIGINAL_HEYA_CD01'], 1);

        // 都道府県
        $data[0]["T03_PREF_NAME"] = Apm_Config_Search::$TODOUFUKEN_NAME_CODE_LIST[$data[0]["T03_TODOUFUKEN_CD"]];
        // 都道府県

        $this->_data['buken'] = $data[0];
        // ショップID
        $this->_data['buken']['LINE_URL'] = $lineUrl;
        $this->_data['buken']['M05_TENPO_LINE_ID'] = $lineId;

        // ショップID
        $this->_footer_data['ftBuken'] = $data[0];
        $this->getAreaData();

        // 同じ建物のほかの物件
        $tatemodel = new Rstdtl_Model_Articledtl();
        $tatemodel_conv = new Rstobi_Model_Converte();

        $tateparams = array();
        // $tatemonoCd = $data[0]['T03_TATEMONO_CD'];
        $tatemonoCd = $data[0]['T03_DAIHYO_TATEMONO_CD'];
        $tateparams['ID'] = $tatemonoCd;
        $tateparams['buken_static'] = '1';
        $tateparams['TatemonoCd'] = $tatemonoCd;

        $tatemonodata = $tatemodel->getArticleDetail($tatemonoCd, $data[0]['T03_HEYA_CD']);
        $c = -1;
        if (isset($tatemonodata) && sizeof($tatemonodata) > 0)
        {
            $tatemodel_conv->dataConverteForBuken($tatemonodata, $tateparams, false, false);
            $tatemodel->convertData($tatemonodata, $tateparams);

            foreach ($tatemonodata as $d)
            {
                $tatemono = $d['T03_DAIHYO_TATEMONO_CD'];
                if ($prev != $tatemono)
                {
                    $c++;
                    $items = array();
                }
                $d['MAX_CHINRYO_STR'] = (intval($d['MAX_CHINRYO']) / 10000);
                $d['MIN_CHINRYO_STR'] = (intval($d['MIN_CHINRYO']) / 10000);
                // OEM判定
                $OemFlg = ((array_key_exists('OemCd', $tateparams) == true) && (is_null($tateparams['OemCd']) == false) && (is_numeric($tateparams['OemCd']) == true));
                // 物件画像取得
                $d['BUKEN_IMAGE'] = Apm_Bukkenitems::GetImageDataMain($d, $OemFlg);
                $d['FULLGAZOU_ICON'] = Apm_Bukkenitems::GetFullGazouIcon($d['BUKEN_IMAGE']);
                // キャンペーン取得
                $heyaCd = $d['T03_HEYA_CD'];
                $ret = $tatemodel->getCamp($heyaCd);
                if (!empty($ret))
                {
                    $d['M94_ITEM_ICON'] = $ret['M94_ITEM_ICON'];
                    $d['M94_CAMPAIGN_URL'] = $ret['M94_CAMPAIGN_URL'];
                    $d['M94_CAMPAIGN_NAME'] = $ret['M94_CAMPAIGN_NAME'];
                }

                $items[] = $d;
                $prev = $tatemono;
            }
            $this->_data['bukenlist'] = $items;
        }
        // 同じ建物のほかの物件

        // ★★★周辺物件探す
        $armodel = new Rstdtl_Model_Articledtl();
        $shuuhens = array();
        $shuuhenBukken = $armodel->getShuuhenBukken($data[0]);
        foreach ($shuuhenBukken as $d)
        {
            $d['BUKEN_IMAGE'] = Apm_Bukkenitems::GetImageDataMain($d, $OemFlg);
            $d["CONVERT_T03_YACHIN"] = Apm_Bukkenitems::GetChinryouStr($d);
            $shuuhens[] = $d;
        }
        $this->_data['shuuhen'] = $shuuhens;
        // ★★★周辺物件探す
    }

    private function getAreaData()
    {
        $prefCd = $this->_data['buken']['T03_TODOUFUKEN_CD'];
        $cityCd = $prefCd . $this->_data['buken']['T03_SHIKUCHOUSON_CD'];

        $model = new Search_Model_Selareasearch();
        $this->_data['PrefInfo'] = $model->getSimpleArea($prefCd);

        $this->_data['PrefName'] = $this->getPrefName($prefCd);
        $this->_data['PrefNameJP'] = $this->getPrefNameJP($prefCd);

        $selcyoModel = new Search_Model_Selcyo(); // 市区町村
        $params = array();
        $params['PrefCd'] = array(
            $prefCd
        );
        $params['CityCd'] = array(
            $cityCd
        );
        $shiku = $selcyoModel->getShikucyosonNameByShikucyosonCd($params);
        $this->_data['ShikuName'] = $shiku[0]['M01_SHIKUCHOUSON_ALIAS'];
        $this->_data['ShikuCd'] = substr($cityCd, 3, 3);
    }

    /**
     * SEO対策用のmetaタグに配置する文章を設定
     */
    protected function setSeoMeta()
    {
        $params = $this->getParameters();

        $t = "%s%s%sの賃貸情報 【アパマンショップ】";
        $des = "賃貸・賃貸住宅の情報ならアパマンショップネットワーク。%s%s%sは%s%sにある%s%sの%sです。今なら賃貸物件を探したお客様から抽選で[現金5万円]が当たる！";

        // 建物名公開の場合
        if ($this->_data['buken']['T03_TATEMONO_NAME_KOUKAI'])
        {
            $tatemonoName = $this->_data['buken']['T03_TATEMONO_NAME'];
        }
        else
        {
            $tatemonoName = $this->_data['buken']['T03_JYUUSHO_TEXT'];
        }

        if ($this->_data['buken']['T03_KAI'])
        {
            $kai = $this->_data['buken']['T03_KAI'] . '階';
            $num = "(" . $params['Code'] . ")";
            $pre = $this->_data['buken']['T03_TODOUFUKEN'];
            $cit = $this->_data['buken']['T03_SHIKUCHOUSON'];
            $are = $this->_data['buken']['CONVERT_T03_SENYUU_MENSEKI'];
            $flo = $this->_data['buken']['CONVERT_T03_MADORI'];
            $tye = $this->_data['buken']['CONVERT_T03_TATEMONO_TYPE'];
        }
        $title = sprintf($t, $tatemonoName, $kai, $num);
        $description = sprintf($des, $tatemonoName, $kai, $num, $pre, $cit, $are, $flo, $tye);
        $keywords = $tatemonoName . $kai . ',';

        $this->_meta_title = $title;
        $this->_meta_description = $description;
        $this->_meta_keywords = $keywords;
        $this->_alternate_url = 'sp/detail/' . $this->_data['buken']['T03_HEYA_CD'] . '/';
    }

}
?>